﻿using CommonNet48.Encrypt;
using Newtonsoft.Json;
using System.IO;
using System.Text;

namespace CommonNet48.SerializerUtils
{
    /// <summary>
    /// Json序列化工具类
    /// </summary>
    public class JsonHelper
    {
        /// <summary>
        /// 默认转换参数
        /// </summary>
        public readonly static JsonSerializerSettings _Settings = new JsonSerializerSettings
        {
            Formatting = Formatting.Indented,
            NullValueHandling = NullValueHandling.Include,
            MissingMemberHandling = MissingMemberHandling.Ignore,
            TypeNameHandling = TypeNameHandling.All,
        };
        /// <summary>
        /// 将对象序列成Json字符串
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="obj">对象</param>
        /// <param name="settings">转换参数</param>
        /// <param name="bEncrypt">是否加密Json字符串，默认不加密</param>
        /// <returns></returns>
        public static string ObjectToJsonString<T>(T obj, JsonSerializerSettings settings = null, bool bEncrypt = false)
        {
            string jsonStr = string.Empty;
            if (settings != null)
            {
                jsonStr = JsonConvert.SerializeObject(obj, settings);
            }
            else
            {
                jsonStr = JsonConvert.SerializeObject(obj);
            }
            if (bEncrypt)
            {
                return DESHelper.Encrypt(jsonStr);
            }
            else
            {
                return jsonStr;
            }
        }

        /// <summary>
        /// 将对象序列化成Json序列化文件(文本编码格式UTF8)
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="obj">对象</param>
        /// <param name="path">Json序列化文件完整路径及名称</param>
        /// <param name="bEncrypt">是否加密Json字符串，默认不加密</param>
        public static void ObjectToJsonFile<T>(T obj, string path, JsonSerializerSettings settings = null, bool bEncrypt = false)
        {
            string jsonStr = JsonHelper.ObjectToJsonString(obj, settings, bEncrypt);
            FileInfo fi = new FileInfo(path);
            if (!Directory.Exists(fi.DirectoryName)) Directory.CreateDirectory(fi.DirectoryName);
            File.WriteAllText(path, jsonStr, Encoding.UTF8);
        }
        /// <summary>
        /// 将对象序列化成Json序列化文件
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="obj">对象</param>
        /// <param name="path">Json序列化文件完整路径及名称</param>
        /// <param name="encoding">字符编码</param>
        /// <param name="bEncrypt">是否加密Json字符串，默认不加密</param>
        public static void ObjectToJsonFile<T>(T obj, string path, Encoding encoding, JsonSerializerSettings settings = null, bool bEncrypt = false)
        {
            string jsonStr = JsonHelper.ObjectToJsonString(obj, settings, bEncrypt);
            FileInfo fi = new FileInfo(path);
            if (!Directory.Exists(fi.DirectoryName)) Directory.CreateDirectory(fi.DirectoryName);
            File.WriteAllText(path, jsonStr, encoding);

        }
        /// <summary>
        /// 将Json字符串反序列成对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="jsonStr">Json字符串</param>
        /// <param name="settings">转换参数</param>
        /// <param name="bDecrypt">反序列化前是否先解密Json字符串，默认不解密</param>
        /// <returns></returns>
        public static T JsonStringToObject<T>(string jsonStr, JsonSerializerSettings settings = null, bool bDecrypt = false)
        {
            if (bDecrypt)
            {
                if (settings != null)
                {
                    return JsonConvert.DeserializeObject<T>(DESHelper.Decrypt(jsonStr), settings);
                }
                else
                {
                    return JsonConvert.DeserializeObject<T>(DESHelper.Decrypt(jsonStr));
                }

            }
            else
            {
                if (settings != null)
                {
                    return JsonConvert.DeserializeObject<T>(jsonStr, settings);
                }
                else
                {
                    return JsonConvert.DeserializeObject<T>(jsonStr);
                }
            }
        }
        /// <summary>
        /// 将Json序列化文件转换成对象(文本编码格式UTF8)
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="path">Json序列化文件完整路径及名称</param>
        /// <param name="settings">转换参数</param>
        /// <param name="bDecrypt">反序列化前是否先解密Json字符串，默认不解密</param>
        /// <returns></returns>
        public static T JsonFileToObject<T>(string path, JsonSerializerSettings settings = null, bool bDecrypt = false)
        {
            if (!File.Exists(path)) return default;
            string jsonStr = File.ReadAllText(path, Encoding.UTF8);
            return JsonHelper.JsonStringToObject<T>(jsonStr, settings, bDecrypt);
        }
        /// <summary>
        /// 将Json序列化文件转换成对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="path">Json序列化文件完整路径及名称</param>
        /// <param name="encoding">字符编码</param>
        /// <param name="settings">转换参数</param>
        /// <param name="bDecrypt">反序列化前是否先解密Json字符串</param>
        /// <returns></returns>
        public static T JsonFileToObject<T>(string path, Encoding encoding, JsonSerializerSettings settings = null, bool bDecrypt = false)
        {
            if (!File.Exists(path)) return default;
            string jsonStr = File.ReadAllText(path, encoding);
            return JsonHelper.JsonStringToObject<T>(jsonStr, settings, bDecrypt);
        }
        /// <summary>
        /// 复制对象(独立副本)
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="obj">要复制的对象</param>
        /// <param name="settings">转换参数</param>
        /// <returns></returns>
        public static T CopyObject<T>(T obj, JsonSerializerSettings settings = null)
        {
            return JsonHelper.JsonStringToObject<T>(JsonHelper.ObjectToJsonString(obj, settings), settings);
        }
    }
}
